import 'dart:async';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart'; // 确保引入 GetX
import '../config/Config.dart';
import '../services/ScreenAdapter.dart';
import '../widget/JdButton.dart';
import '../widget/JdText.dart';
import '../widget/MySnackbar.dart';

class RegisterSecondPage extends StatefulWidget {
  const RegisterSecondPage({super.key});

  @override
  State<RegisterSecondPage> createState() => _RegisterSecondPageState();
}

class _RegisterSecondPageState extends State<RegisterSecondPage> {
  late String tel;
  bool sendCodeBtn = false;
  int seconds = 10;
  late String code;

  @override
  void initState() {
    super.initState();
    tel = Get.arguments["tel"]; // 使用 Get.arguments 获取参数
    _showTimer();
  }

  // 重新发送验证码
  sendCode() async {
    setState(() {
      sendCodeBtn = false;
      seconds = 10;
      _showTimer();
    });
    var api = '${Config.domain}api/sendCode';
    var response = await Dio().post(api, data: {"tel": tel});
    if (response.data["success"]) {
      print(response); // 演示期间服务器直接返回  给手机发送的验证码
    }
  }

  // 倒计时
  _showTimer() {
    Timer? t;
    t = Timer.periodic(const Duration(milliseconds: 1000), (timer) {
      setState(() {
        seconds--;
      });
      if (seconds == 0) {
        // 清除定时器
        t!.cancel();

        setState(() {
          sendCodeBtn = true;
        });
      }
    });
  }

  // 验证验证码
  validateCode() async {
    // var api = '${Config.domain}api/validateCode';
    // var response = await Dio().post(api, data: {"tel": tel, "code": code});
    // if (response.data["success"]) {
    //   Get.toNamed(
    //     "/registerThird",
    //     arguments: {"tel": tel, "code": code},
    //   );
    // } else {
    //   showSnackbar(message: "${response.data["message"]}");
    // }

    // 直接跳转
    Get.toNamed(
      "/registerThird",
      arguments: {"tel": tel, "code": code},
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("用户注册-第二步"),
      ),
      body: Container(
        padding: EdgeInsets.all(ScreenAdapter.width(20)),
        child: ListView(
          children: [
            const SizedBox(height: 50),
            Container(
              padding: const EdgeInsets.only(left: 10),
              child: Text("验证码已经发送到了您的手机$tel, 请输入$tel手机号收到的验证码"),
            ),
            const SizedBox(height: 40),
            Stack(
              children: [
                JdText(
                  text: "请输入验证码",
                  onChanged: (value) {
                    code = value;
                  },
                ),
                Positioned(
                  right: 0,
                  top: 0,
                  child: sendCodeBtn
                      ? ElevatedButton(
                          onPressed: sendCode,
                          child: const Text('重新发送'),
                        )
                      : ElevatedButton(
                          child: Text('$seconds秒后重发'),
                          onPressed: () {},
                        ),
                ),
              ],
            ),
            const SizedBox(height: 20),
            JdButton(
              text: "下一步",
              color: Colors.red,
              height: 74,
              cb: validateCode,
            )
          ],
        ),
      ),
    );
  }
}
